/*连接数据库的界面*/

import java.sql.*;

public class DBAccess {
    private Connection con = null;
    private Statement stm = null;
    public ResultSet rs = null;
    private PreparedStatement prestm = null;

    //Access数据库连接驱动程序
    private String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    //采用ODBC数据源名称访问数据库的方式
    private String url = "";
    private String user = "";
    private String pwd = "";

    public String notes = "数据库操作提示";

    //无参构造函数
    public DBAccess() {
    }

    //有参构造函数：数据源名称，账号，密码
    public DBAccess(String n, String u, String p) {
        this.url = "jdbc:odbc:achievement2";    //自定义数据源名称:achievement2
        this.user = u;    //数据库访问账号，可为空
        this.pwd = p;        //数据库访问账号，可为空
    }

    //实例方法1：实现数据库连接
    public void dbcon() {
        try {
            Class.forName(driver);        //加载数据库驱动程序
            con = DriverManager.getConnection(url, user, pwd);//建立连接
            stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);//?*?*//向数据库发送SQL语句
        }
        //捕获类对象异常
        catch (ClassNotFoundException ec) {
            System.out.println(ec);
        }
        //捕获数据库异常
        catch (SQLException es) {
            System.out.println(es);
        }
        //捕获其他异常
        catch (Exception e) {
            System.out.println(e);
        }
    }

    //实例方法2：查询数据库记录，并返回查询结果的记录集
    public ResultSet dbSelect(String selString) {
        try {
            rs = stm.executeQuery(selString);//执行select语句
        } catch (SQLException es) {
            System.out.println(es);
            notes = "数据库查询出现异常！";
        }
        return rs;
    }

    //实例方法3：更新数据库记录，并返回操作结果提示信息
    public String dbUpdate(String updateString) {
        try {
            prestm = con.prepareStatement(updateString);//生成预编译
            prestm.executeUpdate();    //执行update语句
            notes = "记录更新成功！";
        } catch (SQLException es) {
            System.out.println(es);
            notes = "数据库更新出现异常！";
        }
        return notes;
    }

    //实例方法4：插入数据库记录，并返回操作结果提示信息
    public String dbInsert(String insertString) {
        try {
            prestm = con.prepareStatement(insertString);//生成预编译
            prestm.executeUpdate();    //执行insert语句
            notes = "记录插入成功！";
        } catch (SQLException es) {
            System.out.println(es);
            notes = "数据库插入出现异常！";
        }
        return notes;
    }

    //实例方法5：删除数据库记录，并返回操作结果提示信息
    public String dbDelect(String delString) {
        try {
            prestm = con.prepareStatement(delString);
            prestm.executeUpdate();//执行delect语句
            notes = "记录删除成功！";
        } catch (SQLException es) {
            System.out.println(es);
            notes = "数据库删除出现异常！";
        }
        return notes;
    }

    //实例方法6：关闭数据库连接
    public void dbclose() {
        if (con != null) {
            try {
                rs.close();
                stm.close();
                con.close();
            } catch (Exception e) {
            }
        }
    }
}
